﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using dbPoll_Application.Models;
using dbPoll_Application.ViewModels;
using dbPoll_Application.Helpers;
using iTextSharp.text.pdf;
using iTextSharp.text;

namespace dbPoll_Application.Controllers
{
    public class SystemReportController : Controller
    {
        //
        // GET: /SystemReport/

        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "System Administrator")]
        public ActionResult SystemUtilizationTextual()
        {
            // Get user list
            var userList = UserRepository.GetUsers().Where(user => user.IsDeleted==false).ToList<User>();
            var viewModel = new PollReportViewModel(userList);
            return View(viewModel);
        }

        [Authorize(Roles = "System Administrator")]
        public ActionResult SystemUtilizationGraphical()
        {
            // Get user list
            var userList = UserRepository.GetUsers();

            // Get roles
            var roleList = RoleRepository.GetRoles();

            IDictionary<String, int> userRoles = new Dictionary<String, int>();

            foreach (Role role in roleList)
            {
                userRoles.Add(role.RoleName, 0);
                foreach (User user in userList)
                {
                    foreach (var userRole in user.Roles)
                    {
                        if (userRole.Equals(role))
                        {
                            userRoles[role.RoleName] += 1;
                        }
                    }
                }
            }

            var viewModel = new PollReportViewModel(userRoles);
            return View(viewModel);
        }
        [Authorize(Roles = "System Administrator")]
        public RedirectToRouteResult SystemSessionPdf(String type)
        {
            CodeFile1 pdf = new CodeFile1();
            var userList = UserRepository.GetUsers().Where(user => user.IsDeleted == false).ToList<User>();
            PollReportViewModel viewModel = new PollReportViewModel(userList);
            PdfPTable table = pdf.SystemUtilization(Server.MapPath("/Resources"), viewModel);
            IList<IElement> tab = new List<IElement>();
            tab.Add(table);
            pdf.CreatePDF2(tab, Server.MapPath("/Resources"), type);
            return RedirectToAction("SystemUtilizationPDF");
        }

        [Authorize(Roles = "System Administrator")]
        public ActionResult SystemUtilizationPDF(String urls)
        {
            if (urls!=null)
            {
                CodeFile1 PDF = new CodeFile1();
                Image graph = Image.GetInstance(new Uri("http:" + urls));
                graph.ScaleToFit(490F, 500F);
                IList<IElement> elements = new List<IElement>();
                elements.Add(graph);
                PDF.CreatePDF(elements, Server.MapPath("/Resources"), "System Utilization Report", "");
            }
            return View(new PollReportViewModel());
        }

        [Authorize(Roles = "Poll Administrator")]
        public ActionResult SystemHistoryTextual()
        {
            // Get the user
            var user = UserRepository.GetUserByUsername(User.Identity.Name);
            //var allPolls = new List<Poll>();
            var allPolls = user.ManagedPolls;
            var unique = new List<Poll>();
            foreach (Poll poll in allPolls) { if (!unique.Contains(poll)) unique.Add(poll); }
            allPolls = unique;

            IDictionary<Poll, IList<User>> creatorList = new Dictionary<Poll, IList<User>>();
            IDictionary<Poll, int> attendanceList = new Dictionary<Poll, int>();
            foreach (var poll in allPolls)
            {
                // Get poll creators 
                creatorList.Add(poll, UserRepository.GetCreatorsOfPoll(poll));

                // Get poll attendance
                int attendance = 0;
                foreach (var participant in poll.participants)
                {
                    if (participant.attended)
                    {
                        attendance += 1;
                    }
                }
                attendanceList.Add(poll, attendance);
            }

            var viewModel = new PollReportViewModel(allPolls, creatorList, attendanceList);
            return View(viewModel);
        }
        [Authorize(Roles = "Poll Administrator")]
        public ActionResult SystemHistoryPDF(String image, String location)
        {
            // Get the user
            if (image == "world.jpg") image = Server.MapPath("/Resources")+"\\world.jpg";
            var user = UserRepository.GetUserByUsername(User.Identity.Name);
            //var allPolls = new List<Poll>();
            var allPolls = user.ManagedPolls.Distinct().ToList<Poll>();
            CodeFile1 PDF = new CodeFile1();
            PdfPTable table = PDF.SessionHistory(allPolls, location);
            Image map = Image.GetInstance(new Uri(image));
            map.ScaleToFit(490, 500);
            IList<IElement> elements = new List<IElement>();
            elements.Add(table);
            elements.Add(map);
            PDF.CreatePDF(elements, Server.MapPath("/Resources"), "Session History Report", "");
            return View(new PollReportViewModel());
        }

        [Authorize(Roles = "Poll Administrator")]
        public ActionResult SystemHistoryGraphical()
        {
            return SystemHistoryTextual();
        }
    }
}
